package com.proposition.web.exam;

import com.proposition.commons.exam.exception.ExamException;
import com.proposition.commons.exam.exception.ExamImportException;
import com.proposition.commons.exam.result.Result;
import com.proposition.commons.exam.result.ResultEnum;
import com.proposition.commons.exam.result.ResultUtil;
import com.proposition.commons.exam.util.ExamUtil;
import com.proposition.exam.service.IServiceExamImport;
import org.apache.commons.io.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 10:46 2017/11/8
 * @description: 试题导入
 */
@Controller
@RequestMapping("/exam/examImport")
public class ExamImportController {
    @Autowired
    private IServiceExamImport iServiceExamImport;

    /**
     * @author: 陈玉林
     * @date: 2017/11/8 10:48
     * @description: 进入试题导入界面
     */
    @RequiresPermissions("stdr")
    @GetMapping("/page")
    public String intoExamImportPage() {
        ExamUtil.ensureExamParamIsNotNull();
        return "exam/exam-import";
    }

    /**
     * @author: 陈玉林
     * @date: 2017/11/17 15:34
     * @description: 上传试题
     */
//    @LogAnnotation(moduleName = "试题管理", operate = "试题导入")
    @PostMapping(value = "/upload/post")
    @ResponseBody
    public Result upload(MultipartFile examFile, @RequestParam("category") Integer categoryId, HttpServletRequest request) {
        try {
            String uploadDir = getUploadDir(request);
            File file = new File(uploadDir + examFile.getOriginalFilename());
            FileUtils.writeByteArrayToFile(file, examFile.getBytes());
            if (categoryId == 1) {
                return iServiceExamImport.importExamAcupoint(file.getPath(), categoryId);
            } else if (categoryId == 2 || categoryId == 3 || categoryId == 4 || categoryId == 5 || categoryId == 6|| categoryId == 9|| categoryId == 10|| categoryId == 11) {
                return iServiceExamImport.importExamAnswer10(file.getPath(), categoryId);
            } else if (categoryId == 7) {
                return iServiceExamImport.importExamAcupointPediatric(file.getPath(), categoryId);
            }else if (categoryId == 8) {
                return iServiceExamImport.importExamAcupointAdult(file.getPath(), categoryId);
            } else if (categoryId == 12 || categoryId == 13 || categoryId == 14 || categoryId == 15) {
                return iServiceExamImport.importExamAnswer30(file.getPath(), categoryId);
            }
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        } catch (ExamImportException e1) {
            e1.printStackTrace();
            return e1.getResult();
        }catch (ExamException e2){
            e2.printStackTrace();
            return e2.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        }
    }

    /**
     * 上次文件保存目录
     *
     * @return
     */
    private String getUploadDir(HttpServletRequest request) {
        return request.getServletContext().getRealPath("/upload/emp/");
    }
}
